package com.example.mywanandroid.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mywanandroid.BR;
import com.example.mywanandroid.R;
import com.example.mywanandroid.bean.responsebean.WeChatBean;
import com.example.mywanandroid.databinding.ItemSystemLayoutBinding;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.List;

public class SystemAdapter extends RecyclerView.Adapter<SystemAdapter.SystemViewHolder> {

    private List<WeChatBean> systemList;

    public SystemAdapter(List<WeChatBean> systemList) {
        this.systemList = systemList;
    }

    protected void addListener(View view, WeChatBean bean, int position) {

    }

    protected void addChildListener(View view, WeChatBean bean, List<WeChatBean> parentList, int position) {

    }

    @NonNull
    @Override
    public SystemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.item_system_layout, parent, false);
        return new SystemViewHolder(dataBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull SystemViewHolder holder, int position) {

        WeChatBean bean = systemList.get(position);
        ItemSystemLayoutBinding binding = (ItemSystemLayoutBinding) holder.binding;
        binding.setSystemData(bean);
        addListener(binding.getRoot(), bean, position);
        setChildView(holder.binding.getRoot().findViewById(R.id.rc), bean.getChildren(), holder);
        holder.binding.executePendingBindings();
    }

    private void setChildView(RecyclerView recyclerView, List<WeChatBean> beanList, SystemViewHolder holder) {
        FlexboxLayoutManager manager = new FlexboxLayoutManager(holder.binding.getRoot().getContext());
        manager.setFlexDirection(FlexDirection.ROW);
        manager.setFlexWrap(FlexWrap.WRAP);
        recyclerView.setLayoutManager(manager);
        CommonAdapter<WeChatBean> commonAdapter = new CommonAdapter<WeChatBean>(beanList, R.layout.item_system_tag_layout, com.example.mywanandroid.BR.systemChildren) {
            @Override
            public void addListener(View root, WeChatBean itemData, int position) {
                root.findViewById(R.id.tv_tag).setOnClickListener(view -> {
                    addChildListener(root, itemData, beanList, position);
                });
            }
        };
        recyclerView.setAdapter(commonAdapter);
    }

    @Override
    public int getItemCount() {
        return systemList != null ? systemList.size() : 0;
    }

    public void seetData(List<WeChatBean> data) {
        this.systemList = data;
        notifyDataSetChanged();
    }

    public class SystemViewHolder extends RecyclerView.ViewHolder {

        ViewDataBinding binding;

        public SystemViewHolder(@NonNull ViewDataBinding itemBinding) {
            super(itemBinding.getRoot());
            this.binding = itemBinding;
        }
    }
}
